今天來說圓餅圖,可以快速看出這個項目在全部數量佔的比例。
一樣線建立Drawing,加上pie,這裡的資料格式,只有一層List,不用換算百分比,直接給值自己就會換算了。
完整程式碼
from reportlab.platypus import SimpleDocTemplate
from reportlab.graphics.charts.piecharts import Pie
from reportlab.graphics.shapes import Drawing
drawing = Drawing(200, 100)
dataList = [10, 20, 30, 40, 50, 60, 70]
pie = Pie()
pie.data = dataList
pie.labels = ['A', 'B', 'C', 'D', 'E', 'F', 'G']
drawing.add(pie)
fileName = "example.pdf"
pdfTemplate = SimpleDocTemplate(fileName)
story = []
story.append(drawing)
pdfTemplate.build(story)
結果圖
改顏色是改
slices[num].fillColor: num 是資料的順序
加上label 到區塊的線
sideLabels = 1
完整程式碼
from reportlab.lib import colors
from reportlab.platypus import SimpleDocTemplate
from reportlab.graphics.charts.piecharts import Pie
from reportlab.graphics.shapes import Drawing
drawing = Drawing(200, 100)
dataList = [10, 20, 30, 40, 50, 60, 70]
pie = Pie()
pie.data = dataList
pie.labels = ['A', 'B', 'C', 'D', 'E', 'F', 'G']
pie.sideLabels = 1
pie.slices[0].fillColor = colors.red
pie.slices[1].fillColor = colors.gray
pie.slices[2].fillColor = colors.green
pie.slices[3].fillColor = colors.purple
pie.slices[4].fillColor = colors.blue
pie.slices[5].fillColor = colors.pink
pie.slices[6].fillColor = colors.orange
drawing.add(pie)
fileName = "example.pdf"
pdfTemplate = SimpleDocTemplate(fileName)
story = []
story.append(drawing)
pdfTemplate.build(story)
結果圖
參考資料: